package ibmetp.mis.controller.resource;

import ibmetp.mis.common.Constants;
import ibmetp.mis.vo.ResNode;
import ibmetp.mis.vo.Resource;
import ibmetp.mis.vo.User;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

/**
 * 获取登录用户的资源
 * @author LAN DABIN
 *
 */
public class UserResServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
    public UserResServlet() {
        super();
      
    }

	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("UserResServlet");
		
		String id=request.getParameter("id")!=null?request.getParameter("id"):"1";
		Long pid=Long.valueOf(id);
		
		User user=(User)request.getSession().getAttribute("userinfo");
		JSONArray o=new JSONArray();
		
		//得到pid的子节点
		List<Resource> list=user.getResources();
		for(int i=0;i<list.size();i++)
		{    Resource r=list.get(i);
		   if(r.getParentId().equals(pid))
			{   System.out.println(pid+":"+r.getParentId());
				ResNode rn=new ResNode();
				rn.setId(r.getResourceId());
				rn.setText(r.getResourceName());
				rn.setState("closed");
				//rn.setState("opened");
				o.add(rn);
			}
		}	
		
		//返回JSON数据
		response.setCharacterEncoding(Constants.ENCODING_UTF8);
		response.getWriter().write(o.toString());
		System.out.println(o.toString());
		}

}
